/**
 * @Title: model.js
 * @Description: model
 * @author: Fanchao
 * @date: 2020/9/27 7:56 下午
 * @version V1.0
 */

import { pathToRegexp } from 'path-to-regexp';
import { message } from 'antd';
import {
    reqDetailShop,
} from 'api/index';

export default {
    namespace: 'ShopDetail',
    state: {
        shopDetail: {},
    },
    reducers: {
        setShopDetail(state, { payload: shopDetail }) {
            return { ...state, shopDetail };
        },
    },
    effects: {
        * getShopDetail({ payload }, { call, put, select }) {
            const result = yield call(reqDetailShop, payload);
            if (result.status === 1) {
                const shopDetail = result.data;
                yield put({
                    type: 'setShopDetail',
                    payload: shopDetail
                });
            } else {
                message.error(result.message);
            }
        },
    },
    subscriptions: {
        setup({ dispatch, history }) {
            history.listen(({ pathname, query }) => {
                if (pathToRegexp('/shop-manage/shop/detail').exec(pathname)) {
                    dispatch({
                        type: 'getShopDetail',
                        payload: {
                            shop_id:query.id
                        }
                    });
                }
            });
        },
    },
};
